package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.encrypt.AESProvider;
import com.meichis.mcsappframework.entity.AppVersion;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.MCApplication;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.YIQIYing;
import com.meichis.yndms.repository.ApiUtil;

import java.lang.reflect.Type;
import java.util.Map;

/**
 * @author szw
 * @version 1.0
 * @date 2020/9/9 9:42
 */
public class MainRequestManager extends BaseManager {

    private static MainRequestManager INSTANCE;
    private SharePreferenceUtil util;

    private MainRequestManager() {
        util = SharePreferenceUtil.getInstance();
    }

    public static MainRequestManager getInstance() {
        if (INSTANCE == null) {
            synchronized (MainRequestManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new MainRequestManager();
                }
            }
        }
        return INSTANCE;
    }


    /**
     * 获取版本更新
     *
     * @param showDialog
     * @param AppCode
     * @param appVersion
     */
    public void GetLastVersionInfo(final MutableLiveData<Boolean> showDialog, String AppCode, final MutableLiveData<AppVersion> appVersion) {
        showDialog.setValue(true);

    }

    public void GetOverview(final MutableLiveData<Boolean> showDialog, String partnerId, String staffId, final MutableLiveData<String> data) {
        showDialog.setValue(true);
        Map<String, Object> hs = new ArrayMap<>();
        hs.put("partnerId", partnerId);
        hs.put("staffId", staffId);
        getRootApi().GetOverview(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<String>() {
                }.getType(), new IWebServiceCallback<String>() {
                    @Override
                    public void onSucceed(int sequence, String resultpack, String ReturnInfo, int Return) {
                        showDialog.setValue(false);
                        data.setValue(resultpack);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        MessageTools.showLongToast(error);
                        showDialog.setValue(false);
                    }
                }));
    }

    public void GetFeedbackPopCount(final MutableLiveData<Boolean> showDialog, String partnerId, MutableLiveData<Integer> data) {
        showDialog.setValue(true);
        Map<String, Object> hs = new ArrayMap<>();
        hs.put("PartnerId", partnerId);
        getRootApi().GetFeedbackPopCount(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<String>() {
                }.getType(), new IWebServiceCallback<String>() {
                    @Override
                    public void onSucceed(int sequence, String resultPack, String ReturnInfo, int Return) {
                        showDialog.setValue(false);
                        data.setValue(Return);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        data.setValue(0);
                        showDialog.setValue(false);
                        MessageTools.showShortToast(error);
                    }
                }));
    }

    public void GetAttachmentDownloadURL(String guid, String partnerId) {
        Map<String, Object> hs = new ArrayMap<>();
        hs.put("Guid", guid);
        hs.put("PartnerId", partnerId);
        getRootApi().RequestSFA(getToken(), ApiUtil.getRequestPack(hs, "GetAttachmentDownloadURL"))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<Object>() {
                }.getType(), new IWebServiceCallback<Object>() {
                    @Override
                    public void onSucceed(int sequence, Object resultPack, String ReturnInfo, int Return) {
                        SharePreferenceUtil.getInstance().setStringValue(SharePreferenceUtil.PREFERENCES_DOWNLOADURL, ReturnInfo);
                        MCApplication.DownLoadUrl = ReturnInfo;
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        MessageTools.showShortToast(error);
                    }
                }));
    }

    public void GetYQYLinkUrl(final MutableLiveData<Boolean> showDialog, MutableLiveData<YIQIYing> data) {
        showDialog.setValue(true);
        Map<String, Object> hs = new ArrayMap<>();
        hs.put("UserName", util.getStringValue(SharePreferenceUtil.PREFERENCES_USERNAME));
        getRootApi().GetYQYLinkUrl(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<YIQIYing>() {
                }.getType(), new IWebServiceCallback<YIQIYing>() {
                    @Override
                    public void onSucceed(int sequence, YIQIYing resultPack, String ReturnInfo, int Return) {
                        showDialog.setValue(false);
                        data.setValue(resultPack);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                    }
                }));
    }
}
